require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['jquery','common/pccommons','common/banner'],function($,com,banner){
		var bannerHeight = Math.floor((MediaSize.width() * 1.0)/1920 * 1080);
		$(".banner").css({"height":MediaSize.height() +"px"});
       	$(".service-repair").css({"height":bannerHeight +"px"})
		com.init({headerPos:{}});
		var pageLogic = new PageLogic($,com,banner);
		pageLogic.init();		
	});
});

var PageLogic = function($,com,banner){
	return{
		init:function(){
			var _this = this;
			_this.bindEvents();
			_this.loadStateData();
			_this.setSlideCon();
			com.ajax({
				url:'/index_data.json',
				type:'get',
				success:function(res){
					if(res.success){
						_this.banner(res.data.banners);
						_this.categorySize = res.data.categoryList.length;
						_this.category(res.data.categoryList);
					}
				}
			});
		},
		setSlideCon:function(){
			var winWidth = $(window).width();
			var conWidth = $(".container").width();
			var bothDis = (winWidth - conWidth )/(2.0);
			$(".slide-con").css({"width":(conWidth + bothDis),"margin-left":bothDis});
		},
		banner:function(bannerData){
			var banners = [];
			$(bannerData).each(function(){
				var obj = this;
				banners.push({
					"imgPath":com._Options.domain + obj.imgUrl,
					"url":obj.targetUrl
				});
			});
			banner.init({
				container:$(".banner"),
				anchorType:"line",
				data:banners
			});
		},
		category:function(categorys){
			var html = "";
			if(categorys && categorys.length>0){
				$(categorys).each(function(){
					html += '<li class="slide-pic" tg="' + this.id + '">';
					html += '<div class="slide-img-con"><img src="' + com._Options.domain + this.imageUrl + '"></div>';
					html += '<p>' + this.categoryName + '</p>';
					html += '</li>';
				});
			}
			$(".slide-pics").html(html);
			$(".slide-pic").on("click",function(){
				var tg = $(this).attr("tg");
				window.open("/prodCategorySec.htm?categoryid="+ tg);
			});
		},
		bindEvents:function(){
			var _this = this;
			_this.curidx = 0;
			var width = 21.5 * MediaSize.widthFontsize();
			$(".more-btn").on("click",function(){
				window.open("/prodCategory.htm");
			})
			$(".change-btn").on("click",function(){
				var lis = $(".slide-pics").find("li");
				var size = lis.length;
				if(size <= 4){
					return;
				}
				size = size - 4;
				var tg = $(this).attr("tg");
				if(tg == "prev"){
					if(_this.curidx == 0){
						return;
					}
					_this.curidx -= 1;
				}else if(tg == "next"){
					if(_this.curidx == size){
						return;
					}
					_this.curidx += 1;
				}
				$(".slide-pics").animate({left: -1 * width * _this.curidx + "px"});
			});

		},
		loadStateData:function(){
			var _this = this;
			com.ajax({
				url:'/prov.json',
				data:{type:1},
				type:'get',
				success:function(res){
					if(res.success){
						_this.selectProvince(res.data);
					}
				}
			}); 
		},
		selectProvince:function(provs){
			var _this = this;
			$("#selProvince").html(_this.mkOption(provs,"省","prov"));
			$("#selCity").html(_this.mkOption(null,"市","city"));
			$("#selArea").html(_this.mkOption(null,"区","area"));
			$("#selProvince").on("change",function(){
				_this.selectCity($(this).val());
			})
		},
		selectCity:function(code){
			var _this = this;
			$("#selCity").unbind("change");
			if(code == "0"){
				$("#selCity").html(_this.mkOption(null,"市","city"));
				$("#selArea").html(_this.mkOption(null,"区","area"));
				return;
			}
			com.ajax({
				url:'/city.json',
				data:{type:1,prov:code},
				type:'get',
				success:function(res){
					if(res.success){
						var data = res.data;
						$("#selCity").html(_this.mkOption(data,"市","city"));
						$("#selArea").html(_this.mkOption(null,"区","area"));
					}
				}
			}); 

			$("#selCity").unbind("change").bind("change",function(){
				_this.selectZone(code, $(this).val());
			});
		},
		selectZone:function(pCode,citycode){
			var _this = this;
			$("#selArea").unbind("change");
			if(citycode == "0"){
				$("#selArea").html(_this.mkOption(null,"区","area","area"));
				return;
			}
			com.ajax({
				url:'/area.json',
				data:{type:1,prov:pCode,city:citycode},
				type:'get',
				success:function(res){
					if(res.success){
						var data = res.data;
						$("#selArea").html(_this.mkOption(data,"区","area"));
					}
				}
			}); 
			$("#selArea").unbind("change").bind("change",function(){
				var zone = $(this).val();
				if(zone == "0"){
					return;
				}
				var province = $("#selProvince").val();
				var city = $("#selCity").val();
				var url = "/storeShop.htm?prov=" + province + "&city=" + city + "&area=" + zone;
				window.open(url);
			});
		},
		mkOption:function(data,name,col){
			var options = '<option value="0">' + name + '</option>';
			if(data){
				for(var key in data){
					var partNm = col + "Name";
					options += ' <option value="' +  data[key][col] + '">' + data[key][partNm] + '</option>'
				}
			}
			return options;
		}
	}

}